package com.ruoyi.fetv.util;

import com.ruoyi.common.utils.bean.BeanUtils;

import java.util.List;

/**
 * @ClassName Convert
 * @Description 用来转换不同类型的Bean，方便快速开发
 * @Author 肖润杰
 * @Time 2023/3/10 11:12
 * @Version 1.0
 */
public class Convert {
    /**
     * 转换list
     *
     * @param list   待转换的list
     * @param source 待转换list的Bean类
     * @param target 结果list的Bean类
     * @return 转换后的list
     */
    public static void covertList(List list, Class source, Class target) {
        for (int i = 0; i < list.size(); i++) {
            Object s = list.get(i);
            list.set(i, convert(s, source, target));
        }
    }

    /**
     * 转化Bean
     * * @param object 待转换的Bean
     *
     * @param target 转换的Bean的类
     *               * @return 转换后的Bean
     */
    public static Object convert(Object object, Class target) {
        return convert(object, object.getClass(), target);
    }

    /**
     * 转化Bean
     * * @param object 待转换的Bean
     *
     * @param source 待转换Bean的class
     * @param target 结果Bean的class
     *               * @return 转换后的Bean
     */
    public static Object convert(Object object, Class source, Class target) {
        Object result;
        try {
            result = target.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        BeanUtils.copyBeanProp(result, object);
        return result;
    }

    /**
     * 转化Bean
     * * @param object 待转换的Bean
     *
     * @param target 转换后的Bean
     *               * @return 转换后的Bean
     */
    public static Object convert(Object source, Object target) {
        BeanUtils.copyBeanProp(target, source);
        return target;
    }
}
